package com.yl.chainStore.services.impl.pay

import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.entity.pay.PayCustomerOrderItemDiscountRule
import com.yl.chainStore.entity.pay.PayOrderDiscountRule
import com.yl.chainStore.mapper.pay.PayOrderDiscountRuleMapper
import com.yl.chainStore.services.pay.IPayCouponsRuleService
import com.yl.chainStore.services.pay.IPayOrderDiscountRuleService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.*

@Service
class PayOrderDiscountRuleServiceImpl(
	private val payOrderDiscountRuleMapper: PayOrderDiscountRuleMapper
) : IPayOrderDiscountRuleService {

	companion object {
		private val log = LoggerFactory.getLogger(IPayOrderDiscountRuleService::class.java)
	}


	override fun del(id: Long): ResponseBody {
		log.info("订单优惠设置 ==> 删除 ：id:{}", id)
		val rule = payOrderDiscountRuleMapper.selectById(id) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到订单优惠，请刷新重试"
		)
		rule.enabled = 0
		payOrderDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun resetTime(id: Long, startTime: Date, endTime: Date): ResponseBody {
		log.info("订单优惠 ==> 重设时间 id：{}，startTime：{}，endTime:{}", id, startTime, endTime)
		val rule = payOrderDiscountRuleMapper.selectById(id) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到订单优惠，请刷新重试"
		)
		if (rule.enabled == 0) {
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到订单优惠，请刷新重试"
			)
		}
		rule.startTime = startTime
		rule.endTime = endTime
		rule.status = Status.CLOSE
		payOrderDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun openRule(id: Long): ResponseBody {
		log.info("订单优惠 ==> 开启折扣 id：{}，", id)
		val rule =
			payOrderDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到订单优惠，请刷新重试"
			)
		if (rule.enabled == 0) {
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到订单优惠，请刷新重试"
			)
		}
		rule.status = Status.OPEN

		payOrderDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun closeRule(id: Long): ResponseBody {
		log.info("订单优惠 ==> 关闭折扣 id：{}", id)
		val rule =
			payOrderDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if (rule.enabled == 0) {
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		rule.status = Status.CLOSE
		payOrderDiscountRuleMapper.updateById(rule)
		return returnSuccess()
	}

	override fun updateInfo(rule: PayOrderDiscountRule): ResponseBody {
		log.info("订单项目优惠 ==> 修改基础信息 rule：{}", rule)
		val updateRule =
			payOrderDiscountRuleMapper.selectById(rule.id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if (rule.enabled == 0) {
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		updateRule.title = rule.title
		payOrderDiscountRuleMapper.updateById(updateRule)
		return returnSuccess()
	}

	override fun updateRule(id: Long, rule: Any): ResponseBody {
		log.info("订单项目优惠 ==> 修改规则 id：{}，rule:{}", id, rule)
		val updateRule =
			payOrderDiscountRuleMapper.selectById(id) ?: return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		if (updateRule.enabled == 0) {
			return returnError(
				ErrorCode.DATA_IS_NOT_FIND,
				"未找到项目优惠，请刷新重试"
			)
		}
		updateRule.other = rule
		val validationSelf = updateRule.getOtherContentObj().validationSelf()
		if (validationSelf.bodyIsError()) {
			return validationSelf
		}
		payOrderDiscountRuleMapper.updateById(updateRule)
		return returnSuccess()
	}
}
